package com.iqiyi.pps.epg.api.server.proxy;

import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.api.server.web.broadcast.BroadcastControlWebService;
import com.iqiyi.pps.epg.api.server.web.carousel.CarouselWebService;
import com.iqiyi.pps.epg.api.server.web.channel.ChannelWebService;
import com.iqiyi.pps.epg.api.server.web.encode.EncodeServerWebService;
import com.iqiyi.pps.epg.api.server.web.news.NewsWebService;
import com.iqiyi.pps.epg.api.server.web.program.ProgramWebService;
import com.iqiyi.pps.epg.api.server.web.record.RecordWebService;
import com.iqiyi.pps.epg.api.server.web.record.RecordColRpcService;
import com.iqiyi.pps.epg.api.server.web.statistic.StatisticWebService;
import com.iqiyi.pps.epg.api.server.web.stream.ChannelStreamWebService;
import com.iqiyi.pps.epg.api.server.web.stream.LiveStreamWebService;
import com.iqiyi.pps.epg.api.server.web.ticket.TicketWebService;
import com.iqiyi.pps.epg.api.utils.Constants;
import com.iqiyi.pps.epg.api.utils.CrossDataLocalPool;
import com.iqiyi.pps.epg.api.utils.ServiceUtil;

import javax.ws.rs.core.MultivaluedMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2016/9/9.
 */
public class BaseProxy {
    public static final int RESERVATION_INTERVAL_MILLISECONDS = 600000;
    public static final boolean DEV_MODE_FLAG = Boolean.valueOf(Configuration.getString("qipu.entity.devMode"));
    public static final String ERROR_CLIENT_TIME = "客户端时间不正确（应该为准确时间前后五分钟范围内），授权失败";
    public static final String ERROR_EXIST = "出现异常";
    public static final String INTERFACE_ERROR = "接口异常";
    public static final String AUTH_FAIL = "授权失败";
    public static final String PARAMETER_ERROR = "参数错误";
    public static final String NO_EXECUTE_RULE_ERROR = "无可执行规则";
    public static final String EXECUTE_RULE_RUNNING_ERROR = "规则已经在执行中";
    public static final String FORBIDDEN_ADD_RULE_ERROR = "规则执行中不允许添加";
    public static final String ERROR_CHANNEL_ID_FORMAT = "频道ID格式错误";
    public static final String ERROR_PROGRAM_ID_FORMAT = "节目ID格式错误";
    public static final String ERROR_PROGRAM_QIPU_ID_FORMAT = "节目奇谱ID格式错误";
    public static final String ERROR_DATE_FORMAT = "时间格式错误";

    public static final String DETAIL_RESPONSE_SUC = "操作成功";
    public static final String DETAIL_ERROR_NO_ENTITY = "对象不存在";
    public static final String DETAIL_ERROR_EXCEPTION = INTERFACE_ERROR;
    public static final String DETAIL_UNKNOWN = "未知信息类型";
    public static final String DETAIL_ERROR_WRONG_ENTITY = "对象错误";
    public static final String DETAIL_ERROR_WRONG_RESOURCE = "导播台资源对象错误";
    public static final String DETAIL_API_EXCEPTION = "第三方请求异常";
    public static final int RESPONSE_SUC = 1;
    public static final int ERROR_NO_ENTITY = -1;
    public static final int ERROR_EXCEPTION = -999;
    public static final int ERROR_WRONG_ENTITY = -1001;
    public static final int ERROR_WRONG_RESOURCE = -1002;
    public static final int ERROR_API_EXCEPTION = -1003;
    public static final int ERROR_REPEAT_NAME = -1105;
    public static final int ERROR_REPEAT_PORT = -1106;
    public static final int ERROR_REPEAT_SERVERIP = -1107;
    public static final int ERROR_REPEAT_SN = -1108;
    public static final int ERROR_OPERTION = -1109;
    public static final int ERROR_HOST = -1110;
    public static final int ERROR_RESOURCE_PLAYCOUNTS = -3001;
    public static final int ERROR_MAINRESOURCE_NONE = -3002;
    public static final int ERROR_SET_MAINRESOURCE = -3003;


    protected ProgramWebService.Iface getProgramService() {
        return ServiceUtil.getProgramService();
    }

    protected ChannelWebService.Iface getChannelService() {
        return ServiceUtil.getChannelService();
    }

    protected CarouselWebService.Iface getCarouselService() {
        return ServiceUtil.getCarouselService();
    }

    protected StatisticWebService.Iface getStatisticService() {
        return ServiceUtil.getStatisticService();
    }

    protected EncodeServerWebService.Iface getEncodeServerService() {
        return ServiceUtil.getEncodeServerService();
    }


    protected BroadcastControlWebService.Iface getBroadcastService() {
        return ServiceUtil.getBroadcastService();
    }

    protected ChannelStreamWebService.Iface getChannelStreamWebService() {
        return ServiceUtil.getChannelStreamWebService();
    }

    protected LiveStreamWebService.Iface getLiveStreamService() {
        return ServiceUtil.getLiveStreamService();
    }

    protected NewsWebService.Iface getNewsService() {
        return ServiceUtil.getNewsService();
    }
    protected TicketWebService.Iface getTicketWebService() {
        return ServiceUtil.getTicketWebService();
    }

    protected RecordWebService.Iface getRecordWebService() {
        return ServiceUtil.getRecordWebService();
    }
    protected RecordColRpcService.Iface getRecordColRpcService(){
        return ServiceUtil.getRecordColRpcService();
    }

    protected void writeProgramLog(long pid, int type, String user, String descr) {
        ServiceUtil.writeProgramLog(pid, type, user, descr);
    }

    protected void writeChannelLog(long cid, int type, String user, String descr) {
        ServiceUtil.writeChannelLog(cid, type, user, descr);
    }

    protected void writeDirectorLog(int director, int type, String user, String descr) {
        ServiceUtil.writeDirectorLog(director, type, user, descr);
    }


    protected void writeVodLog(int channelId, int type, String user, String descr) {
        ServiceUtil.writeVodLog(channelId, type, user, descr);
    }

    protected void writeTicketLog(long entityId,int type,String user,String descr){
        ServiceUtil.writeTicketLog(entityId, type, user, descr);
    }

    protected String getStrParam() {
        Map<String, Object> params = CrossDataLocalPool.get().getParams();
        return params == null ? null : params.toString();
    }

    protected String getCodeStr(int retCode) {
        String retValue = null;
        switch (retCode) {
            case RESPONSE_SUC:
                retValue = DETAIL_RESPONSE_SUC;
                break;
            case ERROR_NO_ENTITY:
                retValue = DETAIL_ERROR_NO_ENTITY;
                break;
            case ERROR_EXCEPTION:
                retValue = DETAIL_ERROR_EXCEPTION;
                break;
            case ERROR_WRONG_ENTITY:
                retValue = DETAIL_ERROR_WRONG_ENTITY;
                break;
            case ERROR_WRONG_RESOURCE:
                retValue = DETAIL_ERROR_WRONG_RESOURCE;
                break;
            case ERROR_API_EXCEPTION:
                retValue = DETAIL_API_EXCEPTION;
                break;
            case ERROR_REPEAT_NAME:
                retValue = "频道名称已存在";
                break;
            case ERROR_REPEAT_PORT:
                retValue = "频道端口号已存在";
                break;
            case ERROR_REPEAT_SERVERIP:
                retValue = "服务器IP已存在";
                break;
            case ERROR_REPEAT_SN:
                retValue = "序列号已存在";
                break;
            case ERROR_OPERTION:
                retValue = "操作失败";
                break;
            case ERROR_HOST:
                retValue = "服务器验证失败";
                break;
            case ERROR_RESOURCE_PLAYCOUNTS:
                retValue = "点播播次错误，已同步后端数据";
                break;
            case ERROR_MAINRESOURCE_NONE:
                retValue = "请先设置主信号源";
                break;
            case ERROR_SET_MAINRESOURCE:
                retValue = "点播播放期间不能设置主信号源";
                break;
            default:
                retValue = DETAIL_UNKNOWN + retCode;
                break;
        }
        return retValue;
    }

    public String getResponseWithJsonp(String code, Object value, String msg) {
        String result = Constants.getJsonResponse(code, value, msg);
        if (isJsonpRequest()) {
            return getJsonpParameter() + "(" + result + ")";
        }
        return result;
    }

    public String getResponseWithJsonp(String code, Object value, String msg, boolean isThriftEntity) {
        String result = Constants.getResponse(code, value, msg).toJsonString();
        if (isJsonpRequest()) {
            return getJsonpParameter() + "(" + result + ")";
        }
        return result;
    }

    public boolean isJsonpRequest() {
        MultivaluedMap<String, String> queryParameters = ServiceUtil.getUriInfo().getQueryParameters();
        if (queryParameters.containsKey("callback")) {
            return true;
        }
        return false;
    }

    public String getJsonpParameter() {
        MultivaluedMap<String, String> queryParameters = ServiceUtil.getUriInfo().getQueryParameters();
        if (queryParameters.containsKey("callback")) {
            return queryParameters.getFirst("callback");
        }
        return null;
    }
}
